package com.cn.springcloudwechat.common.utils;
import com.cn.springcloudwechat.model.YmChildGrowthRepo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class AgeCount {
    static Logger logger=LoggerFactory.getLogger(AgeCount.class);
    /**
     * 计算孩子年龄
     * @param fromDate
     * @return
     */
    public static String getAge(Date fromDate) {
        Calendar cal = Calendar.getInstance();
        Date toDate = cal.getTime();

        Calendar from = Calendar.getInstance();
        from.setTime(fromDate);
        Calendar to = Calendar.getInstance();
        to.setTime(toDate);
        int fromYear = from.get(Calendar.YEAR);
        int fromMonth = from.get(Calendar.MONTH) + 1;
        int fromDay = from.get(Calendar.DAY_OF_MONTH);
        System.out.println("children:" + fromYear + "-" + fromMonth + "-" + fromDay);
        int toYear = to.get(Calendar.YEAR);
        int toMonth = to.get(Calendar.MONTH) + 1;
        int toDay = to.get(Calendar.DAY_OF_MONTH);
        System.out.println("now:" + toYear + "-" + toMonth + "-" + toDay);
        int year = toYear - fromYear;
        int month = toMonth - fromMonth;
        int day = toDay - fromDay;
        if (fromYear == toYear && fromMonth == toMonth) {
            day = toDay - fromDay;
            if (day < 0) {
                System.out.println("未出生的孩子");
            }
        } else if (fromYear == toYear) {
            month = toMonth - fromMonth;
            if (month < 0) {
                System.out.println("未出生的孩子");
            } else {
                day = toDay - fromDay;
                if (day >= 0) {

                } else {
                    month--;
                    int daysOfMonth = getDaysOfMonth(to.getTime());
                    day = daysOfMonth + toDay - fromDay;
                }
            }
        } else if (fromYear != toYear) {
            if (fromYear > toYear) {
                System.out.println("未出生的孩子");
            } else if (fromYear < toYear) {
                year = toYear - fromYear;
                day = toDay - fromDay;
                if (toDay >= fromDay) {
                    if (toMonth >= fromMonth) {
                        month = toMonth-fromMonth;
                    } else {
                        year--;
                        month = 12 + toMonth - fromMonth;
                    }
                } else {
                    int daysOfMonth = getDaysOfMonth(to.getTime());
                    day = daysOfMonth + toDay - fromDay;
                    if (fromMonth == toMonth) {
                        month = 12 + toMonth - fromMonth - 1;
                        year--;
                    } else if (fromMonth > toMonth) {
                        month = fromMonth - toMonth - 1;
                    } else {
                        month = 12 + toMonth - fromMonth - 1;
                        year--;
                    }
                }
            } else {
                System.out.println("未想到的判断");
            }
        }
        System.out.println(year + "-" + month + "-" + day);
        String age =year + "-" + month ;
        if(year<0){
            logger.info("孩子年计算错误");
            age="孩子年计算错误";
        }
        if(month<0){
            logger.info("孩子月计算错误");
            age="孩子月计算错误";
        }
//        if(day<0){
//            logger.info("孩子日计算错误");
//            age="孩子日计算错误";
//        }
        return age;
    }

    public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

//    public static void main(String[] args) {
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//        ParsePosition pos = new ParsePosition(0);
//        Calendar cal = Calendar.getInstance();
//        Date strtodate = formatter.parse("2017-11-01", pos);
//        cal.setTime(strtodate);
//        getAge(strtodate);
//        String[] result = getAge(strtodate).split("-");
//        System.out.println(result[1]);
//
//    }

    /**
     * 获取体重指数
     * @param ymChildGrowthRepo
     * @param w 体重
     * @return
     */
    public static Double getWeightStar(YmChildGrowthRepo ymChildGrowthRepo,Double w){
        Double weightStar = 0.0;
        String weightRang = ymChildGrowthRepo.getWeightRang();
        String[] weight = weightRang.split("-");
        Double weightPrefix = new Double(weight[0]);
        Double weightSuffix = new Double(weight[1]);
        Double standardWeight = (weightSuffix-weightPrefix)/10;
        if(w >= weightPrefix && w< (weightPrefix+standardWeight*1)){
            weightStar = 0.5;
        }else if(w >= (weightPrefix+standardWeight*1) && w< (weightPrefix+standardWeight*2)){
            weightStar = 1.0;
        }else if(w >= (weightPrefix+standardWeight*2) && w< (weightPrefix+standardWeight*3)){
            weightStar = 1.5;
        }else if(w >= (weightPrefix+standardWeight*3) && w< (weightPrefix+standardWeight*4)){
            weightStar = 2.0;
        }else if(w >= (weightPrefix+standardWeight*4) && w< (weightPrefix+standardWeight*5)){
            weightStar = 2.5;
        }else if(w >= (weightPrefix+standardWeight*5) && w< (weightPrefix+standardWeight*6)){
            weightStar = 3.0;
        }else if(w >= (weightPrefix+standardWeight*6) && w< (weightPrefix+standardWeight*7)){
            weightStar = 3.5;
        }else if(w >= (weightPrefix+standardWeight*7) && w< (weightPrefix+standardWeight*8)){
            weightStar = 4.0;
        }else if(w >= (weightPrefix+standardWeight*8) && w< (weightPrefix+standardWeight*9)){
            weightStar = 4.5;
        }else if(w >= (weightPrefix+standardWeight*9)){
            weightStar = 5.0;
        }
        return weightStar;
    }

    /**
     * 获取身高指数
     * @param ymChildGrowthRepo
     * @param h 身高
     * @return
     */
    public static Double getHeightStar(YmChildGrowthRepo ymChildGrowthRepo,Double h){
        Double heightStar = 0.0;
        String heightRang = ymChildGrowthRepo.getHightRang();
        String[] height = heightRang.split("-");
        Double heightPrefix = new Double(height[0]);
        Double heightSuffix = new Double(height[1]);
        Double standardHeight = (heightSuffix-heightPrefix)/10;
        if(h >= heightPrefix && h< (heightPrefix+standardHeight*1)){
            heightStar = 0.5;
        }else if(h >= (heightPrefix+standardHeight*1) && h< (heightPrefix+standardHeight*2)){
            heightStar = 1.0;
        }else if(h >= (heightPrefix+standardHeight*2) && h< (heightPrefix+standardHeight*3)){
            heightStar = 1.5;
        }else if(h >= (heightPrefix+standardHeight*3) && h< (heightPrefix+standardHeight*4)){
            heightStar = 2.0;
        }else if(h >= (heightPrefix+standardHeight*4) && h< (heightPrefix+standardHeight*5)){
            heightStar = 2.5;
        }else if(h >= (heightPrefix+standardHeight*5) && h< (heightPrefix+standardHeight*6)){
            heightStar = 3.0;
        }else if(h >= (heightPrefix+standardHeight*6) && h< (heightPrefix+standardHeight*7)){
            heightStar = 3.5;
        }else if(h >= (heightPrefix+standardHeight*7) && h< (heightPrefix+standardHeight*8)){
            heightStar = 4.0;
        }else if(h >= (heightPrefix+standardHeight*8) && h< (heightPrefix+standardHeight*9)){
            heightStar = 4.5;
        }else if(h >= (heightPrefix+standardHeight*9)){
            heightStar = 5.0;
        }
        return heightStar;
    }

    public static void main(String[] args) {
        YmChildGrowthRepo ymChildGrowthRepo = new YmChildGrowthRepo();
        ymChildGrowthRepo.setWeightRang("10-30");
        Double h = 14.0;
        Double star = getWeightStar(ymChildGrowthRepo,h);
        System.out.println(star);
    }
}

